package com.xmall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xmall.bean.vo.IssueList;
import com.xmall.bean.table.Issue;
import com.xmall.bean.table.IssueExample;
import com.xmall.mapper.IssueMapper;
import com.xmall.service.IssueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class IssueServiceImpl implements IssueService {

    @Autowired
    IssueMapper issueMapper;

    @Override
    public IssueList<Issue> selectIssueList(Integer page, Integer limit,String question, String sort, String order) {
        IssueExample issueExample = new IssueExample();
        IssueList<Issue> issueIssueList = new IssueList<>();
        IssueExample.Criteria criteria = issueExample.createCriteria();
        if(question !=null){
            criteria.andQuestionLike("%" + question +  "%" );
        }

        criteria.andDeletedEqualTo(false);
        PageHelper.startPage(page, limit);
        issueExample.setOrderByClause(sort + " " + order);
        List<Issue> issues = issueMapper.selectByExample(issueExample);
        PageInfo<Issue> objectPageInfo = new PageInfo<>();
        long total = objectPageInfo.getTotal();



        issueIssueList.setItems(issues);
        issueIssueList.setTotal(total);

        return issueIssueList;
    }

    @Override
    public Issue insertIssue(Issue issue) {

        issue.setAddTime(new Date());
        issue.setUpdateTime(new Date());

        issueMapper.insertSelective(issue);

        return issue;
    }

    @Override
    public Integer deleteIssue(Issue issue) {

        issue.setDeleted(true);
        issueMapper.updateByPrimaryKeySelective(issue);
        return 0;
    }

    @Override
    public Issue updateIssue(Issue issue) {

        issue.setUpdateTime(new Date());

        issueMapper.updateByPrimaryKey(issue);
        return issue;


    }
}
